#include "dpclock.h"

#include <QtGui/QtGui>
#include <QtCore/QtCore>


DPClock::DPClock( QGraphicsItem * parent) : QGraphicsObject(parent)
{
    mNums.push_back(0);
}

void DPClock::setTime(int nSeconds)
{
    mNums.clear();

    QString str = QString("%1").arg(nSeconds);

    foreach(QChar c, str)
    {
        mNums.push_back(c.digitValue());
    }
	update();
}

QRectF DPClock::boundingRect() const
{
    return QRectF(QPointF(0, 0), this->mClockPixmap.size() + mNums.size() * this->mNumPixmaps[0].size());
}

void DPClock::paint(QPainter *painter, const QStyleOptionGraphicsItem *, QWidget *)
{
     painter->drawPixmap(QPointF(0,0), this->mClockPixmap);
     for(int i = 0; i<mNums.size(); i++)
     {
        painter->drawPixmap(QPointF(mClockPixmap.size().width() + i*mNumPixmaps[0].size().width(), 20), this->mNumPixmaps[mNums[i]]);
     }
}

void DPClock::initPixmapsIni(const QString& cfgName)
{
    QSettings setting(cfgName, QSettings::IniFormat);

    setting.beginGroup("Clock");
    this->mClockPixmap.load(setting.value("clock").toString());
    for(int i=0; i<10; i++)
    {
        QString texName;
        QString key = QString("num%1").arg(i);
        texName = setting.value(key).toString();
        this->mNumPixmaps[i].load(texName);
    }
    setting.endGroup();

    mTimer = new QTimer(this);
    QObject::connect(mTimer, SIGNAL(timeout()), this, SLOT(updateTimer()));
}

void DPClock::updateTimer()
{

    static unsigned long timer = 0;
    timer += 1;

    this->setTime(timer);
}

void DPClock::start()
{
    mTimer->start(1000);
}

void DPClock::stop()
{
    mTimer->stop();
}
